package com.tianqi.ui.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class BaseDAO {
    private String username = "root";
    private String password = "root";
    private String url ="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true";
    private String driverClass = "com.mysql.jdbc.Driver";

//    获取一个有效的连接
    public Connection getCon(){
        try{
            Class.forName(driverClass);
            Connection con = DriverManager.getConnection(url,username,password);
            return con;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
//    关闭连接
    public void closeAll(ResultSet rs, Statement st, Connection con) {
        try {
            if(rs!=null)
                rs.close();
            if(st!=null)
                st.close();
            if(con!=null)
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
        // 将编译期异常封装成运行时异常，向上抛出。
        }
    }

}
